package model;

import entities.User;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;

public class ChoseDoctorTableModel extends AbstractTableModel {

    ArrayList<User> list;
    ArrayList<User> disp;

    public ChoseDoctorTableModel() {
        list = new ArrayList<User>();
        disp = new ArrayList<User>();
    }

    public ChoseDoctorTableModel(ArrayList<User> list) {
        this.list = new ArrayList<User>(list);
        this.disp = new ArrayList<User>(list);
    }

    @Override
    public int getRowCount() {
        return disp.size();
    }

    @Override
    public int getColumnCount() {
        return 4;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        User user = disp.get(rowIndex);
        switch (columnIndex) {
            case 0:
                return user.getLoginID();
            case 1:
                return user.getName();
            case 2:
                return user.getPhone();
            case 3:
                return user.getEmailAddress();
            default:
                return null;
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return String.class;
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0:
                return "ID";
            case 1:
                return "Name";
            case 2:
                return "Phone";
            case 3:
                return "Email Address";
            default:
                return null;
        }
    }

    public boolean addAll(ArrayList<User> list) {
        boolean returnVal = this.list.addAll(list);
        if (returnVal) {
            fireTableDataChanged();
        }
        return returnVal;
    }

    public boolean addElement(User user) {
        boolean returnVal = list.add(user);
        if (returnVal) {
            fireTableDataChanged();
        }
        return returnVal;
    }

    public User getAt(int index) {
        return list.get(index);
    }

    public void clear() {
        list.clear();
    }

    public void filter(int departmentID) {
        if (departmentID != 0) {
            disp.clear();
            for (User u : list) {
                if (u.getDepartmentID() == departmentID) {
                    disp.add(u);
                }
            }
        } else {
            disp.addAll(list);
        }
        fireTableDataChanged();
    }
}
